/*============================================================================*/
/*                   CONTINENTAL AUTOMOTIVE SYSTEMS                           */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            HALDebouncer.c
* version:         1.1
* created_by:      Lizette Acosta/Rodrigo Casados
* date_created:    June 22 2013
*=============================================================================*/
/* DESCRIPTION : Debouncer and Button Event Validation                        */
/*============================================================================*/
/* FUNCTION COMMENT : main function of Window Lifter                          */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.1      | 22/June/2013 |                              | Lizette Acosta   */
/*                                                           Rodrigo Casados  */
/*============================================================================*/
#include "stdtypedef.h"
#include "HALDebouncer.h"


/*============================================================================*/
/*                           vfn_ReadButtons                                  */
/*============================================================================*/
void vfn_ReadButtons(e_StateFlag * ptr_ButtonID)
{	
	static T_UBYTE slub_InvalidEventCnt = 0; /* Each time an individual Button is pressed the counter increments on 1*/
									 	     /* in order to avoid multiple/simultaneous button press */
	if(BTN_1 || BTN_2)
	{
		if(BTN_1)
		{
			*ptr_ButtonID = BTTN1;
			slub_InvalidEventCnt++;
		}
		
		if(BTN_2)
		{
			*ptr_ButtonID = BTTN2;
			slub_InvalidEventCnt++;
		}
		
		if(slub_InvalidEventCnt != 0)	/* In case that there was an event detected */
		{
			if(slub_InvalidEventCnt >= 2)	 
			{
				*ptr_ButtonID = INVALID;	/* If more than one button was pressed, the event is considered invalid*/
			}
			else
			{
			}
		}
		
		else
		{
			*ptr_ButtonID = TO_DEFINE;		/* If no event was detected, status remains as To Define */
		}
	}
	
	else
	{
		*ptr_ButtonID = TO_DEFINE;			/* If no event was detected, status remains as To Define */
	}
	
	slub_InvalidEventCnt = 0;	/* Clears Counter */
}



/*============================================================================*/
/*                              u8fn_Deboouncer                               */
/*============================================================================*/

void vfn_Deboouncer(e_StateFlag *leub_ButtonID)
{
	static e_StateFlag u8_PrevButtonID = TO_DEFINE;
	static e_PIT_Status seub_PIT_10ms_Status = NEW_EVENT;
	e_StateFlag u8_EventResult = INVALID;
	
	/* Verify global Flag */
	if(gub_DebouncerFlag)	/* This flag is set only when debouncer's time lapse is completed */
	{
		seub_PIT_10ms_Status = TIME_LAPSE_COMPLETED;	/* Updates status for state machine */
		gub_DebouncerFlag = FALSE;						/* Clears flag */
	}
	
	
	/* State Machine */
	switch(seub_PIT_10ms_Status)
	{
		/* New Event */
		case NEW_EVENT:			/* New Button Event to Debounce */
			u8_PrevButtonID = (*leub_ButtonID);	/* Stores current ID for futher comparison */
			vfn_Start_10ms_PIT();					/* Starts 10ms timer (interruption) */
			u8_EventResult = TO_DEFINE;				/* In the meantime, the return value will be "Invalid" */
			seub_PIT_10ms_Status = WAITING;			/* Changes status to "Waiting" until 10ms timer is running */
		break;
		
		/* Waiting */
		case WAITING:					/* Waiting for the 10msec to elapse */
			u8_EventResult = TO_DEFINE; 	/* In the meantime, the return value will be "Invalid" */
		break;
		
		/* Time Lapse Completed (10ms)*/
		case TIME_LAPSE_COMPLETED:	/* This state is reached only when debouncer's time lapse is completed */
			if(u8_PrevButtonID == *leub_ButtonID)	/* Compares previous and current Button ID */
			{
				u8_EventResult = *leub_ButtonID;		/* If it's the same, it's considered a Valid Event */
			}											/* and the Button ID (selected mode) is returned */
			
			else
			{
				u8_EventResult = INVALID;				/* If previous and current ID doesn't match, */
			}											/* the event is considered invalid */
		
			seub_PIT_10ms_Status = NEW_EVENT;			/* Updates status for further debouncing */
		break;
		
		/* Default */
		default:
			u8_EventResult = INVALID;					/* Default condition */
			seub_PIT_10ms_Status = NEW_EVENT;			/* Resets State Machine for further debouncing */
		break;
	}
	
	*leub_ButtonID = u8_EventResult;
}

